import { NextApiRequest, NextApiResponse } from 'next'
import {withIronSessionApiRoute} from 'iron-session/next'
import {prepareConnection} from 'db/index'
import { User,Article,Tag } from 'db/entity/index'
import {ironOptions} from 'config/index'
import { ISession} from 'pages/api/index'
import { EXCEPTION_ARTICLE } from 'pages/api/config/codes'

export default withIronSessionApiRoute(get,ironOptions)

async function get(req: NextApiRequest, res: NextApiResponse) {
    const sesssion: ISession = req.session
    const {userId} = sesssion
    // 连接数据库
    const db = await prepareConnection()
    const tagRepo = db.getRepository(Tag)
    // 判断用户是否登录，未登录则返回错误
    const followTags = await tagRepo.find({
        relations: ['users'],
        where: (qb) => {
            qb.where('user_id = :id', { 
                id:Number(userId) 
            })
        }
    })

    const allTags = await tagRepo.find({
        relations: ['users'],
    })
   
    res?.status(200).json({code:0,msg:'发布成功',data:{followTags,allTags}})

    // if(user){
    //     // 返回数据
    //     res?.status(200).json({code:0,msg:'发布成功',data:user})
    // }else{
    //     // 返回错误
    //     res?.status(200).json({...EXCEPTION_ARTICLE.PUBLISH_FAILED})
    // }
}